package com.easy.controller;

import com.easy.bean.Inventory;
import com.easy.service.InventoryService;
import com.easy.util.Page;
import com.easy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/api/inventory")
public class InventoryController {

    @Autowired
    InventoryService inventoryService;

    // 新增库存
    @PostMapping
    public Result add(@RequestBody Inventory inventory) {
        return Result.success(inventoryService.add(inventory));
    }

    // 编辑库存
    @PutMapping("/{id}")
    public Result edit(@PathVariable("id") Integer id, @RequestBody Inventory inventory) {
        inventory.setId(id);
        return Result.success(inventoryService.edit(inventory));
    }

    // 删除库存
    @DeleteMapping("/{id}")
    public Result del(@PathVariable("id") Integer id) {
        return Result.success("删除成功", inventoryService.del(id));
    }

    // 查询所有库存（可模糊查询）
    @GetMapping
    public Result all(
            @RequestParam(name = "itemType", required = false, defaultValue = "") String itemType,
            @RequestParam(name = "location", required = false, defaultValue = "") String location
    ) {
        return Result.success(inventoryService.all(itemType, location));
    }

    // 查询单个库存
    @GetMapping("/{id}")
    public Result one(@PathVariable("id") Integer id) {
        return Result.success(inventoryService.one(id));
    }

    // 分页查询库存
    @GetMapping("/page")
    public Result page(Page page, Inventory inventory) {
        inventoryService.page(page, inventory);
        return Result.success(page);
    }
}
